home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SPACE 2
/
SPACE - Library 2 - Volume 1.iso
/
apps
/
82
/
tutorc.eng
< prev
next >
Wrap
Text File
|
1986-11-20
|
8KB
|
155 lines
0660103030566
9[....................................................]
üC,THELANGUAGE
WÇelcometothisfirstlessoninüCÇlanguage.Thisseriesof
articlesismeantasalearningmethodforbeginningprogrammers
inüCÇ.Iwillhowevertakeforgrantedthatthereaderalreadyhas
somenotionssuchasknowledgeofloops,conditionnalexpressions,
etc.
Hereisthefirstwarning.TheüCÇlanguageisnotaneasy
language.Itisaflexiblelanguage,generatingfastandcompact
codebutitisveryhardtodebug.Errormessages,whenthereare
any(...),arenotextremelysignificantandmayreferto
correctlywritteninstructions(therealerrormightinfactbe
anywhere...).
Asecondwarning.TheexistingversionsofüCÇavailableonthe
üSTÇarecompiledlanguages.Thismeansthattheyarenotdirectly
executablelikeüSTBasicÇorüAtariLOGOÇ.(Thoseareinterpreted
languages).Youcannot,forexample,typealineofcodeandsee
(orobserve)itseffectimmediately.Manystepsarenecessary
beforeyouobtainareadytorunprogram.Tofindtherightsteps,
toexecutetheminorder,willtakemoretimeduringthefirst
triesthanwritingtheprogramitself.
Ifthisintroductionhasn'tdiscouragedyou,youareready
foryourfirstplunge.
First,youneedaprogrameditor.Manyofthemareavailable
onthemarket:üMinceÇorüMicro-EmacsÇ,orsimpler;awordprocessing
programsuchasü1st-WordÇ.Withthelatter,youmustbecarefulnot
toselecttheWPmode,becauseitwillinsertinthetextsome
controlcharactersthatwillnotberecognizedbythecompiler.
Typethefollowinglines:
#üinclude"osbind.h"
main()Ç
{ü
Cconws("ThisismyfirstCprogram");
Cconin();
Ç}
Savethistextfileunderthename"üMYFIRST.CÇ"
Let'snowanalysethisprogramindetail.Thefirstline,
#üincludeÇ"üosbind.hÇ",containsthe#character.Thissymbolsignals
tothecompilerthearrivalofapre-processorcommand.Itmustbe
usedonlyinthismanner(unlessitappearsinasentencebetween
quotes).TheüincludeÇpre-processorcommandincludestheüosbind.hÇ
fileinsidethecode.Thisfilecontainsdefinitionsforthe
üCconwsÇandüCconinÇofourprogramand,inamoregeneralway,it
containsthedefinitionsoftheüCPM-68KÇoperatingsystem.This
typeoffilewhichcontainsdefinitions,ischaracterizedbyits
ü.hÇextension.Forexample,theüvdibind.hÇandüaesbind.hÇfilesare
definitionsfortheüVDIÇandüAESÇ;theyareentrypointstotheüGEMÇ.
Noticethatthenameofthefilemustbebetweenquotes.
Thesecondline,ümain()Ç,tellsthecompilerthattheprogram
executionmustbeginhere.Theümain()Çfunctionmayinfactbe
anywhereinsidethetext.InüCÇ,exceptionmadeofthepre-
processorcommands,onlyfunctionsexist.Functionswhichcall
otherfunctionswhichcallyetmorefunctions,etc...Thefirst
functiontobeexecutedisalwaysümain()Çevenifitisnotthe
firstonetoappearinthetext.
Afunctionisdistinguishablebythepresenceofparenthesis
whichalwaysfollowthenameofthefunction.Theseparenthesis
containtheargument(s)ofthefunction.Theümain()Çfunctiontakes
noargumentsso,the(compulsary)parenthesisareempty.The
üCconwsÇfunctioncontainsanargument,thesentence:"üThisismy
firstCprogramÇ",thisfunctionwillwritethelineoftextonthe
screenatthecurrentcursorlocation,here,theupperleft
corner.(üCconswÇistheabbreviationfor'üConsolewritestringÇ'.)
Thequotesdelimitingthesentencearenecessaryaswellasthe
semicolonendingthelineüCconsw(Ç"üThisismyfirstCprogramÇ"ü);
ÇTheabsenceofthispunctuationwillreturnanerrormessage
duringcompilation.
Thesemicoloniswhatiscalled,inprogrammer'sslang,a
delimiter.Itspecifiesthateverythingprecedingitalltheway
backtotheprevioussemicolonorbacktothestartofthe
functionproper,isafunctionthatiscompletebyitself.The
semicolondelimitatesthebodyoftheprecedingfunction.
Thefollowingfunction,üCconin();Çwaitsforsomecharacterto
betypedonthekeyboardanddisplaysitonthescreen(ifitisa
printableASCIIcharacter).üCconinÇistheabbreviationfor
'üConsoleInputÇ'.Thereisanotherfunctionthatdoesthesamejob,
butwithoutprintingthecharacter;itsnameisüCnecin()Ç.Thisone
mustbeusedduringapauseinsideaprogram,forexample.
Onthelastlinewefindaclosingbracket'ü}Ç',whichis
alwayspairedtoanopeningbracket'ü{Ç'.Theyareusedtolimit
thefieldoftheümain()Çfunction,'ü{Ç'markingthestartand'ü}Ç'at
theend.TheyareusedinthesamemannerintestsusingüwhileÇor
üifÇ-üthenÇ-üelseÇaswewillseelater.
Allthatisleftforustodonowisto"compile"this
program.Thecompilationistheprocessbywhichthecomputer
translatestheprogramtextinbinaryinstructions(i.e.machine
language)andplacestheseinstructionsinafilewithaü.PRGÇ
suffix.Thisfileisdirectlyexecutable,meaningthatitdoesn't
needanotherprogramtorunit,(forexampletakeaüLOGOÇprogram
whichneedstheüLOGO.PRGÇfiletowork).
Thecompilationstepsneededvaryfromonecompilerto
another.ForthecompilerthatcomeswiththeüAtariÇüToolkitÇ,the
stepstofolloware:
üCP68KMYFIRST.CMYFIRST.I
ÇüC068MYFIRST.IMYFIRST.1MYFIRST.2MYFIRST.3-F
C168MYFIRST.1MYFIRST.2MYFIRST.S
AS68-L-UMYFIRST.S
Ç
üLINK68[U]MYFIRST.68K=APSTART,MYFIRST,GEMLIB
RELMODMYFIRST
Ç
ThefirstfourstepsproducethefileüMYFIRST.SÇ,üÇwhichisan
assemblylanguagefile(accordingtoüMotorolaÇmnemoniccodes).The
twolaststepswillgeneratethedesiredfile,üMYFIRST.PRGÇ.
ü ÇThenamesatthestartofeachofthefirstfourlinesabove
correspondtothenamesoffilesonthe"üCompilerDiskÇ"andthe
lasttwocorrespondtonamesoffilesonthe"üLinkerDiskÇ".You
mustexecutetheseprogramsbyenteringtheparametersthatfollow
thenameinthelineinthefollowingmanner:'click'onceonthe
nameoftheprogram,selectüInstallApplicationÇintheüOPTION
Çchoice(menubar)ü,Çandwriteintherequiredparameters.
AsecondmethodconsistsincallingtheüCOMMAND.PRGÇprogram
(itisincludedineachofthediskettesmentionned)andwritein
thelinesasabove.
Athirdmethod,theonemostoftenused,callsfortheü.BATÇ
filemethod.Thesefilescontaintheequivalentofthelines
above.Forexample,theüC.BATÇfilecontainsthefirstfourlines
above;weexecutethisfilebycallingtheüBATCH.PRGÇandbytyping
üC.MYFIRSTÇ(andnotüC.BATMYFIRST.CÇasmighthavebeenexpected).
Forthetwolastlines,calltheüBATCH.PRGÇwiththeparameters
üLINKCMYFIRSTÇ(thisone'snamewillvaryaccordingtoyourversion
oftheüTOOLKITÇ).
Whenyouhavefinished,yourfirstprogramwillappearunder
thenameüMYFIRST.PRGÇ.Tryit...Inthenextlesson,wewill
examinehowtouseloopsandconditionnalexpressions.
üAlainBirtz
March1986Ç